package helloword;

import com.rabbitmq.client.*;
import org.junit.Test;
import utils.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author by Jak
 * @date 2020/7/6
 */
public class Consumer {

    public static void test() throws IOException, TimeoutException {
/*        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("10.67.167.183");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/springCloud");
        connectionFactory.setUsername("jak");
        connectionFactory.setPassword("123456");

        //创建连接对象
        Connection connection = connectionFactory.newConnection();*/

        //通过工具类获取连接对象
        Connection connection = RabbitMQUtils.getConnection();

        //创建通道
        Channel channel = connection.createChannel();

        //通道绑定对象, 生产者和消费者要对应，既持久化，都持久化
        channel.queueDeclare("hello", true, false, true, null);

        //消费消息
        //参数1：消费哪个队列的消息 队列名称
        //参数2：开启消息的自动确认机制
        //参数3：消费时的回调接口
        channel.basicConsume("hello", true, new DefaultConsumer(channel) {
            @Override //最后一个参数：消息队列中取出的消息
            public void handleDelivery (String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body) throws IOException {
                System.out.println("new String(body) = " + new String(body));
            }
        });
    }

    public static void main(String[] args) throws IOException, TimeoutException{
        test();
    }
}
